package nbcp.db.mongo.entity

import org.bson.types.ObjectId
import org.springframework.data.annotation.Id
import nbcp.base.comm.*
import nbcp.base.extend.AsInt
import nbcp.db.mongo.*

import java.util.*
import nbcp.base.extend.FromJson
import nbcp.base.utils.CodeUtil
import nbcp.base.utils.RecursionReturnEnum
import nbcp.base.utils.RecursionUtil
import nbcp.base.utils.SpringUtil
import nbcp.db.*
import java.net.Inet4Address
import java.time.LocalDateTime
import kotlin.collections.LinkedHashMap

/**
 * Created by udi on 17-4-18.
 */

data class ProductCard(
        var id:String = "",
        var name:String = "",
        var logo: IdUrl = IdUrl(),
        var category: IdCodeName = IdCodeName(),
        var guidePrice: Int = 0,
        var salePrice: Int = 0,
        var stock: Int = 0
)
//生产商商品表。
data class ProductSkuStockPrice(
        var skuDefine: String = "", //逗号分隔的字符串
        var stock: Int = 0,
        var code: String = "",      //编码，也可能是一个范围。
        var activityPrice: Int = -1, //如果产品设置了活动, 则表示活动价格,如果为 -1 ,则使用 salePrice
        var salePrice: Int = -1 //分。
)

//内嵌实体
data class SysCorporation_ReceiveAddress(
        var id: String = CodeUtil.getCode(),
        var alias: String = "", //别名
        var userName: String = "",
        var city: IntCodeName = IntCodeName(),
        var address: String = "",
        var mobile: String = "",
        var email: String = "",
        var default: Boolean = false,
        var createAt: LocalDateTime = LocalDateTime.now()) {}



//内嵌实体: 订单每一步状态的信息
data class OrderInfo_OperationItem(
        var createAt: LocalDateTime = LocalDateTime.now(),
        var createBy: IdName = IdName(),
        var status: OrderStatusEnum = OrderStatusEnum.Created, //新状态
        var remark: String = ""
)


/**
 * 购买的商品信息
 */
data class OrderInfo_ProductItem(
        var id: String = CodeUtil.getCode(),
        var product: IdNameUrl = IdNameUrl(),
        var skuDefine: String = "",         // 如: 金,128G

        var number: Int = 0, //数量
        var amount: Int = 0, //总额
        var price: Int = 0  //单价
)

//发票
data class InvoiceData(
        var id: String = CodeUtil.getCode(),
        var name: String = "",           //个人名称,或,企业名称
        var type: InvoiceTypeEnum = InvoiceTypeEnum.None,
        var default: Boolean = false,
        var corpNumber: String = "",  //统一社会信用代码
        var address: String = "",
        var phone: String = "",
        var bankName: String = "",
        var bankAccount: String = "",
        var license: IdUrl = IdUrl(),    //营业执照
        var bankImage: IdUrl = IdUrl(),    //开户行证明.
        var taxpayer: IdUrl = IdUrl()    //一般纳税人证明.
)

/**
 * 支付信息
 */
data class OrderInfo_PayInfo(
        var remark: String = "", //备注
        var mobile: String = "", //联系方式
        var payType: PayTypeEnum = PayTypeEnum.Alipay, //支付方式

        var invoice: InvoiceData = InvoiceData(), //发票信息

        var payAmount: Int = 0,     //支付总金额(含运费,扣除优惠.)
        var freight: Int = 0,       //运费
        var paySysMessage: String = ""     //支付回调消息.
)


data class MenuDefine(
        var id: String = CodeUtil.getCode(),
        var code: String = "",
        var name: String = "",
        var url: String = "",
        var css: String = "",
        var pages: LinkedHashMap<String, String> = linkedMapOf(),
        var subMenus: MutableList<MenuDefine> = mutableListOf()
) {
    companion object {
        fun loadFromJson(json: String): MenuDefine {
            if (json.isEmpty()) {
                return MenuDefine()
            }

            return json.FromJson<MenuDefine>();
        }


    }

    /**
     * 当前及子节点下所有的 url + pages 集合.
     */
    val permissions: List<String>
        get() {
            var list = mutableListOf<String>()

            RecursionUtil.execute(mutableListOf<MenuDefine>(this), { it.subMenus }, { it, parent, index ->
                list.add(it.url)
                list.addAll(it.pages.keys);
                RecursionReturnEnum.Go
            });

            return list.distinct();
        }
}

